package cn.mumzone.pos.presenter;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.event.DownloadChangeEvent;
import cn.mumzone.pos.event.JPushEvent;
import cn.mumzone.pos.event.MoreViewChangeEvent;
import cn.mumzone.pos.event.RefreshMemberLevelEvent;
import cn.mumzone.pos.model.AppVersionEntity;
import cn.mumzone.pos.model.BannerEntity;
import cn.mumzone.pos.model.BannerResult;
import cn.mumzone.pos.model.LevelListEntity;
import cn.mumzone.pos.model.source.AppRepository;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.model.source.UserRepository;
import cn.mumzone.pos.presenter.contract.HomeContract;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by qiuzhenhuan on 2019/7/12.
 */

public class HomePresenter extends AbsPresenter<HomeContract.View, AppRepository> implements HomeContract.Presenter {

    UserRepository userRepository;
    private int count;
    AppRepository appRepository;
    MemberRepository memberRepository;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        userRepository = new UserRepository();
        appRepository = new AppRepository();
        memberRepository = new MemberRepository();
    }

    @Override
    public void checkAppUpdate() {
        ApiCallBack<AppVersionEntity> callBack = new ApiCallBack<AppVersionEntity>() {
            @Override
            public void onSuccess(AppVersionEntity result) {
                mView.showCheckAppUpdate(result);
            }

            @Override
            public void onError(BaseResponse response) {

            }
        };
        mRepo.updateCheck(callBack);
        addSubscrebe(callBack);
    }


    @Override
    public void initEvents() {
        Subscription moreViewChangeListener = RxBus.getDefault()
                .toObservable(MoreViewChangeEvent.class)
                .subscribe(new Action1<MoreViewChangeEvent>() {
                    @Override
                    public void call(MoreViewChangeEvent moreViewChangeEvent) {
                        mView.showMoreViewChanged(moreViewChangeEvent.getType());
                    }
                });
        addSubscrebe(moreViewChangeListener);
        Subscription refreshMemberLevelListener = RxBus.getDefault()
                .toObservable(RefreshMemberLevelEvent.class)
                .subscribe(new Action1<RefreshMemberLevelEvent>() {
                    @Override
                    public void call(RefreshMemberLevelEvent refreshMemberLevelEvent) {
                        getMemberLevel();
                    }
                });
        addSubscrebe(refreshMemberLevelListener);
    }

    @Override
    public void requestBannerList() {

        ApiCallBack<BannerResult> callBack = new ApiCallBack<BannerResult>() {
            @Override
            public void onSuccess(BannerResult bannerResponse) {
                mView.showBannerList(bannerResponse.getList() == null ? new ArrayList<BannerEntity>() : bannerResponse.getList());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showBannerList(new ArrayList<BannerEntity>());
            }
        };
        mRepo.findPostersListPage(callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void doSaveVersion(String version) {
        mRepo.saveRemoteVersion(version);
    }

    public void setDownloadingListener() {
        Subscription subscription = RxBus.getDefault()
                .toObservable(DownloadChangeEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<DownloadChangeEvent>() {
                    @Override
                    public void call(DownloadChangeEvent downloadChangeEvent) {
                        mView.onDownloadingProgress(downloadChangeEvent.getProgress());
                    }
                });
        addSubscrebe(subscription);
    }

    public void onClickCount() {
        if (count < 10) {
            count++;
        } else {
            ApiRequest.init("http://test3.mumzone.cn/");
            appRepository.saveBaseUrl("http://test3.mumzone.cn/");
            mView.onDebugModeOpened();
        }
    }

    public void onClickLong() {
        if (count < 10) {
            count++;
        } else {
            ApiRequest.init("http://192.168.2.79/");
            appRepository.saveBaseUrl("http://192.168.2.79/");
            mView.onDebugModeOpened();
        }
    }

    public void getMemberLevel() {
        ApiCallBack<List<LevelListEntity>> levelCallback = new ApiCallBack<List<LevelListEntity>>() {

            @Override
            public void onSuccess(List<LevelListEntity> levelList) {
                memberRepository.saveMemberLevel(levelList);

            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        memberRepository.getMemberLevel(AppConstants.TEMP_SHOP_ID, levelCallback);
        addSubscrebe(levelCallback);

    }
}
